//获取应用实例
const app = getApp();

Page({
  data: {
    mony:'0.00',
    qurd:'',
    moneyNum:'',
    // 组件所需的参数
    nvabarData: {
      showCapsule: 1, //是否显示左上角图标   1表示显示    0表示不显示
      title: '结算中心', //导航栏 中间的标题
    },
    // 此页面 页面内容距最顶部的距离
    height: app.globalData.height * 2 + 20,
  },
  onLoad(opt) {
    let that = this;
    that.setData({
      mony:opt.money,
      qurd:app.globalData.qurd
    })
  },
  inputedit:function(event){
    this.setData({
      moneyNum: this.money(event.detail.value)  //money匹配金额输入规则，返回输入值
    });
  },
  /**
   * @method: 金额输入限制
   * @params: val接收number值
   */
  money(val) {
    let num = val.toString(); //先转换成字符串类型
    if (num.indexOf('.') == 0) { //第一位就是 .
      num = '0' + num
    }
    num = num.replace(/[^\d.]/g, "");  //清除“数字”和“.”以外的字符
    num = num.replace(/\.{2,}/g, "."); //只保留第一个. 清除多余的
    num = num.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
    num = num.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3'); //只能输入两个小数
    if (num.indexOf(".") < 0 && num != "") {
      num = parseFloat(num);
    }
    return num
  },
//上传收款二维码
unloadshouimg(){
  let that = this;
  wx.chooseImage({
    complete: (res) => {
      console.log(res)
      
      // 上传二维码到服务器
      let key = Math.random().toString(36).substr(2); //生成一个随机字符串的文件名
      let url = app.globalData.URL+'common/upload'
      wx.uploadFile({
       url: url,
       filePath: res.tempFilePaths[0],
       header: { 
         "Content-Type": "multipart/form-data",
         'accept': 'application/json',
         'Authorization': getApp().globalData.token //若有token，此处换上你的token，没有的话省略
         },
       name: 'file',
       formData:{
        'key': key//这里是为文件设置上传后的文件名
       },
       success: function(res){
        let lvarr = JSON.parse(res.data)
        app.globalData.qurd = lvarr.url;
        that.setData({
          qurd:app.globalData.qurd
        })
       },
       fail:function (res) {
         console.log(res)
       }
      })
    },
  })
},
// 调用微信提现
giveMeprice(){
  let that = this;
  let qurd = that.data.qurd;
  if(qurd==''){
    wx.showModal({
      title: '请上传收款二维码',
      
    })
  }else{
    let url = app.globalData.URL+'system/extract?money='+that.data.moneyNum+'&qrCode='+qurd;
  let data = {};
      app.wxRequest('POST', url, data, (res) => {
        if(res.code!=200){
          wx.showModal({
            title: res.msg
          })
        }else{
          wx.showModal({
            title: '提现成功',
            success(){
              that.onLoad();
              wx.switchTab({
                url: '/pages/index/index',
              })
            }
          })
        }
      }, (err) => {
        console.log(err.errMsg)
      })
  }
  
},

  // 查看提现记录
  gowithdrawal_record:function(){
    wx.navigateTo({
      url: '/modelB/pages/my/withdrawal_record/withdrawal_record',
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  },

  
})
